package com.qiongqi.datadir.service.impl;

import com.qiongqi.datadir.config.DataDirConfig;
import com.qiongqi.datadir.domain.BucketQuota;
import com.qiongqi.datadir.mapper.BucketQuotaMapper;
import com.qiongqi.datadir.service.IBucketQuotaService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.Instant;
import java.util.List;

import static com.qiongqi.datadir.utils.uuid.IdUtils.fastSimpleUUID;
import static java.util.Objects.nonNull;
import static org.apache.commons.lang3.ArrayUtils.isNotEmpty;
import static org.apache.commons.lang3.StringUtils.isNotBlank;

@Slf4j
@Service
@Transactional
public class BucketQuotaServiceImpl implements IBucketQuotaService {

    @Autowired
    private BucketQuotaMapper bucketQuotaMapper;

    @Autowired
    private DataDirConfig dataDirConfig;

    /**
     * 查询可信空间
     *
     * @param id 可信空间主键
     * @return 可信空间
     */
    @Override
    public BucketQuota selectBucketQuotaById(String id) {
        return isNotBlank(id) ? bucketQuotaMapper.selectBucketQuotaById(id) : null;
    }

    /**
     * 查询可信空间
     *
     * @param bucketName 可信空间名称
     * @return 可信空间
     */
    @Override
    public BucketQuota selectBucketQuotaByName(String bucketName) {
        return isNotBlank(bucketName) ? bucketQuotaMapper.selectBucketQuotaByName(bucketName) : null;
    }

    /**
     * 查询可信空间列表
     *
     * @param bucketQuota 可信空间
     * @return 可信空间集合
     */
    @Override
    public List<BucketQuota> selectBucketQuotaList(BucketQuota bucketQuota) {
        if(nonNull(bucketQuota)){
            bucketQuota.setCreateBy(dataDirConfig.getName());
        }
        return nonNull(bucketQuota) ? bucketQuotaMapper.selectBucketQuotaList(bucketQuota) : List.of();
    }

    /**
     * 新增可信空间
     *
     * @param bucketQuota 可信空间
     * @return 结果
     */
    @Override
    public int insertBucketQuota(BucketQuota bucketQuota) {
        int ret = -1;
        if (nonNull(bucketQuota)) {
            Instant currentTime = Instant.now();
            bucketQuota.setId(fastSimpleUUID());
            bucketQuota.setCreateBy(dataDirConfig.getName());
            bucketQuota.setCreateTime(currentTime);
            bucketQuota.setUpdateBy(dataDirConfig.getName());
            bucketQuota.setUpdateTime(currentTime);
            ret = bucketQuotaMapper.insertBucketQuota(bucketQuota);
        }
        return ret;
    }

    /**
     * 修改可信空间
     *
     * @param bucketQuota 可信空间
     * @return 结果
     */
    @Override
    public int updateBucketQuota(BucketQuota bucketQuota) {
        int ret = -1;
        if (nonNull(bucketQuota)) {
            bucketQuota.setUpdateBy(dataDirConfig.getName());
            bucketQuota.setUpdateTime(Instant.now());
            ret = bucketQuotaMapper.updateBucketQuota(bucketQuota);
        }
        return ret;
    }

    /**
     * 批量删除可信空间
     *
     * @param ids 需要删除的可信空间主键集合
     * @return 结果
     */
    @Override
    public int deleteBucketQuotaByIds(String[] ids) {
        return isNotEmpty(ids) ? bucketQuotaMapper.deleteBucketQuotaByIds(ids) : 0;
    }

    /**
     * 删除可信空间信息
     *
     * @param id 可信空间主键
     * @return 结果
     */
    @Override
    public int deleteBucketQuotaById(String id) {
        return isNotBlank(id) ? bucketQuotaMapper.deleteBucketQuotaById(id) : 0;
    }

    /**
     * 检查是否已存在同名桶
     *
     * @param bucketName
     * @return
     */
    @Override
    public boolean checkBucketNameIfExisted(String bucketName) {
        return isNotBlank(bucketName) ? nonNull(bucketQuotaMapper.selectBucketQuotaByName(bucketName)) : true;
    }
}
